package com.glebusheg.client
{
	import flash.geom.Rectangle;


	/**
	 * @author �������������
	 */
	public class RpcFakeUtil
	{
		
		private static function parseFarm(xml:XML):Object
		{
			var rc:Rectangle = GameData.getFarmSize(0);
			var dx:int = rc.x;
			var dy:int = rc.y;
			
			var world:Object = { };
			world.objects = [];
			
			var node:XML;
			var iter:XML;
			var obj:Object;
			var plant:Object;
			
			var id:int = 0;
			for each (iter in xml.object)
			{
				obj = { };
				obj.angle = (iter.hasOwnProperty("@angle")) ? int(iter.@angle) : 0; 
				obj.counter = (iter.hasOwnProperty("@counter")) ? int(iter.@counter) : 0;
				
				if (iter.hasOwnProperty("@item"))
					obj.item = iter.@item.toString();
				
				if (iter.hasOwnProperty("@phase"))
					obj.phase = iter.@phase.toString();
					
				obj.timer = (iter.hasOwnProperty("@timer")) ? int(iter.@timer) : 0;
				
				if (iter.hasOwnProperty("@x")) obj.x = int(iter.@x) + dx;
				if (iter.hasOwnProperty("@x")) obj.y = int(iter.@y) + dy;
				
				node = iter.plant[0];
				if (node)
				{
					plant = { };
					plant.counter = (node.hasOwnProperty("@counter")) ? int(node.@counter) : 0;
					
					if (node.hasOwnProperty("@item"))
						plant.item = node.@item.toString();
					if (node.hasOwnProperty("@phase"))
						plant.phase = node.@phase.toString();
						
					plant.timer = (node.hasOwnProperty("@timer")) ? int(node.@timer) : 0;
					
					obj.plant = plant;
				}
				
				obj.id = (world.objects as Array).length + 1;
				world.objects.push(obj);
			}
			
			world.nextID = (world.objects as Array).length + 1;
			return world;
		}
		
		private static function addHomeFarm(data:Object):void
		{
			data.world = parseFarm(GameData.INIT_FARM_XML.home_farm[0]);
		}

		private static function addFriendFarm(data:Object):void
		{
			data.world = parseFarm(GameData.INIT_FARM_XML.friend_farm[0]);
		}

		public static function createInitialize():Object
		{
			var data:Object = new Object();

			var xml:XML = GameData.INIT_FARM_XML.settings[0];
			var node:XML;
			var iter:XML;
			
			data.ssid = "asdasd1234534523461234";
			data.randomSeed = 0;
			
			data.date = int((new Date()).time / 1000);
			
			//if (xml.hasOwnProperty("@tutorial")) data.tutorial = int(xml.@tutorial);
			
			data.player = new Object();
			data.player.id = NetworkManager.network.uid;
			//data.player.avatar = { body:"favatar1", suit:"fsuit1", hat:"fhat1", staff:"fstaff1" };
			data.player.coins = (xml.hasOwnProperty("@coins")) ? int(xml.@coins) : 0;
			data.player.cash = (xml.hasOwnProperty("@cash")) ? int(xml.@cash) : 0;
			data.player.xp = (xml.hasOwnProperty("@xp")) ? int(xml.@xp) : 0;
			data.player.sp = 10;
			data.player.energy = (xml.hasOwnProperty("@energy")) ? int(xml.@energy) : 0;
			data.player.maxEnergy = (xml.hasOwnProperty("@energy")) ? int(xml.@energy) : 0;
			data.player.energyTime = 50000;
			//data.player.farmSizeLevel = 0;
			data.player.inventory = {deco_fountain:1, buy_energy_1:2, buy_energy_5:5, ho_hint_1:30, ho_hint_4:30};
			data.player.unlocked = [];

			//addHomeFarm(data);
			//addHomeFarmPerf(data);

			data.users = [];
			for each (var uid:String in NetworkManager.network.appFriends)
				data.users.push({id:uid, xp:int(Math.random()*120), gift:int(Math.random()*2)});
			/*data.users.push({id:"PLAYER2", xp:1, gift:1});
			data.users.push({id:"PLAYER3", xp:5, gift:1});
			data.users.push({id:"PLAYER4", xp:3, gift:1});
			data.users.push({id:"PLAYER5", xp:4, gift:1});
			data.users.push({id:"PLAYER6", xp:9, gift:0});
			data.users.push({id:"PLAYER7", xp:6, gift:0});
			data.users.push({id:"PLAYER8", xp:0});
			data.users.push({id:"PLAYER9", xp:0});
			data.users.push({id:"PLAYER10", xp:10});
			data.users.push({id:"PLAYER11", xp:120});
			data.users.push({id:"PLAYER12", xp:13});
			data.users.push({id:"PLAYER13", xp:20});*/

/*			data.quests = [];
			data.sales = [];

			// QUEST and SALES init from xml
			if(GameData.INIT_FARM_XML && GameData.INIT_FARM_XML.settings[0])
			{
				node = xml.quests[0];
				if(node && GameFeature.isAvailable(GameFeatureType.QUESTS))
				{
					for each (iter in node.quest)
					{
						if(iter.hasOwnProperty("@name"))
							data.quests.push({name:(iter.@name.toString()), tasks:[0, 0, 0]});
					}
				}
				
				node = xml.sales[0];
				if(node &&  GameFeature.isAvailable(GameFeatureType.SALES))
				{
					for each (iter in node.sale)
					{
						if(iter.hasOwnProperty("@id"))
							data.sales.push({id:(iter.@id.toString()), progress:0});
					}
				}
			}

			// DAILY BONUS init from xml
			if(!data.hasOwnProperty("tutorial") || data.tutorial == 0)
			{ 
				data.messages = {};
				data.messages.dbonus = "bonus1";
				//data.messages.explorer_start = {href:"http://explorersite.com"};
			}*/
			
			//data["result"] = 0;
			//data["message"] = "ддааыва";
			
			return data;
		}
	}
}
